//
// Created by wyh on 2025-07-25 07:11.
//

#ifndef FRAMEWORK_CPP_SERIALIZABLE_H
#define FRAMEWORK_CPP_SERIALIZABLE_H

namespace wyh {
    namespace serialize {
        class DataStream;
        class Serializable
        {
        public:
            //序列化
            virtual void serialize(DataStream &stream) const = 0;

            //反序列化
            virtual bool unserialize(DataStream &stream) = 0;
        };

        #define SERIALIZE(...)                                      \
        void serialize(DataStream &stream) const         \
        {                                                           \
            char type = DataStream::CUSTOM;              \
            stream.write((char *) &type, sizeof(char));             \
            stream.write_args(__VA_ARGS__);                         \
        }                                                           \
                                                                    \
        bool unserialize(DataStream &stream)             \
        {                                                           \
            char type;                                              \
            stream.read(&type,sizeof(char));                            \
            if (type != DataStream::CUSTOM)              \
            {                                                       \
                return false;                                       \
            }                                                       \
            stream.read_args(__VA_ARGS__);                          \
            return true;                                            \
        }
    } // namespace serializable
} // namespace wyh

#endif //FRAMEWORK_CPP_SERIALIZABLE_H
